package com.example.sonny.dutest;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.sonny.Constraint;
import com.example.sonny.dutest.interfaces.BaseActivityI;
import com.github.nkzawa.socketio.client.Socket;

public class ChatActivity extends BaseActivity implements BaseActivityI{

    private EditText et_message;
    private EditText et_chat_to;
    private Button btn_send;
    private String target;
    private String content;
    private Socket mSocket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        target = "desktop";
        preInit();
        initView();
        initData();
        initListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_chat, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void preInit() {

    }

    @Override
    public void initView() {
        et_message = (EditText)findViewById(R.id.et_chat);
        et_chat_to = (EditText)findViewById(R.id.et_chat_to);
        btn_send =(Button)findViewById(R.id.btn_chat_sent);

    }

    @Override
    public void initData() {
        mSocket = Constraint.getMSocket();

    }

    @Override
    public void initListener() {
        btn_send.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_chat_sent:
                target = et_chat_to.getText().toString();
                content = et_message.getText().toString();
                sendMessage();
                break;
        }

    }

    private void sendMessage(){
        String[] data = {target,content};
        mSocket.emit("new message",data);

    }
}
